<?php
/**
 *  Create On 2014-11-11
 *  Author Yiwei
 *  QQ:1006629314
 **/
include_once "DB.php";
include_once "TRANS.php";
include_once "item2image.php";
include_once "modify.php";
/*
 * 图片转换部分
 */

$sourceDbConf = array (
				'host' => "192.168.81.7",
				'database' => "XDF_DB",
				'user' => "root",
				'pw' => 'howdo2014',
				'charset' => 'utf8'
		
		);


header ( "Content-type:text/html;charset=utf-8" );
set_time_limit ( 0 );
ob_end_clean ();
ob_implicit_flush ( 1 );

$sourceDb = new DB ( $sourceDbConf );

$sourceDb->open ();

//$sourceFilter = 'item_id > 0 AND create_time IN
// (SELECT MAX(create_time) FROM `xdf_cache` GROUP BY item_id)';
$sourceFilter = null;
$sourceNum = $sourceDb->getTotal ( 'xdf_cache', $sourceFilter );
$sourceIndex = $sourceDb->setIndex ( 'id', 'xdf_cache', $sourceFilter );

echo '总共将转换' . $sourceNum . '条数据<br>';
echo date('Y-m-d H:i:s').'<br>';
$conv = new item2imgae();
for($i = 0; $i < $sourceNum; $i ++) {
	echo '第' . $i . '条=>';
	$data = $sourceDb->getRow ( $sourceIndex, $i );
	//var_dump($data);
	$conv->convert($data['item_id'], 512, 0, 10000);
	echo '<br>';
}
echo date('Y-m-d H:i:s').'<br>';
echo '全部数据转换完成';
$sourceDb->close ();


/*
 *数据转换部分 
 */
/*
$sourceDbConf = array (
		'host' => "192.168.81.10:3306",
		'database' => "howdo",
		'user' => "root",
		'pw' => 'howdo2014',
		'charset' => 'utf8' 
);

$targetDbConf = array (
		'host' => "192.168.80.23:3306",
		'database' => "testitem",
		'user' => "root",
		'pw' => 'howdo2014',
		'charset' => 'utf8'
);

header ( "Content-type:text/html;charset=utf-8" );
set_time_limit ( 0 );
ob_end_clean ();
ob_implicit_flush ( 1 );

$sourceDb = new DB ( $sourceDbConf );
$targetDb = new DB ( $targetDbConf );

$sourceDb->open ();
$targetDb->open ();

 $sourceFilter = 'item_id > 2222 AND item_id<9999 AND create_time IN
 (SELECT MAX(create_time) FROM `cache` GROUP BY item_id) ORDER BY item_id';
//$sourceFilter = null;
$sourceNum = $sourceDb->getTotal ( 'cache', $sourceFilter );
$sourceIndex = $sourceDb->setIndex ( 'id', 'cache', $sourceFilter );

echo '总共将转换' . $sourceNum . '条数据<br>';

for($i = 0; $i < $sourceNum; $i ++) {
	echo '第' . $i . '条=>';
	$data = $sourceDb->getRow ( $sourceIndex, $i );
	//修改cache数据
	$modify = new modify($data);
	$newdata = $modify->doModify();
	
	//迁移cache数据
	$sql = 'INSERT INTO `xdf_cache` (item_id,json,sub_id,type_id,create_time)
			VALUES ("'.$newdata['item_id'].'","'.addslashes($newdata['json']).'",
					"'.$newdata['sub_id'].'","'.$newdata['type_id'].'",NOW())';
	echo 'trans cache:'.$targetDb->insert($sql).'succ->';
	
	//数据转换
	$findjoin = 'SELECT * FROM `join` WHERE `cache_id` = '.$data['id'];
	$join = $sourceDb->fetchRow($findjoin);
	$tran = new TRANS ( $newdata,$join, $targetDb );
	$tran->trans ();
	//exit;
	echo '<br>';
}

echo '全部数据转换完成';
$sourceDb->close ();
$targetDb->close ();
*/

	
	